package com.windfone.web.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.model.Recommend;
import com.windfone.model.RecommendType;
import com.windfone.service.RecommendService;

public class RecommendAction extends ActionSupport {

	private RecommendService recommendService;
	private List<RecommendType>	recommendTypes;
	private List<Recommend> recommends;
	private int recommendTypeId;
	private List records;
	private List oldRecords;
	private String tableName;
	private String beanName;
	private String beginDate;
	private String endDate;
	private String fields;
	private String fieldsLabel;
	private String recordIdsStr;
	private String positionSeqsStr;
	private String recommendIdsStr;
	public String getTableName() {
		return tableName;
	}
 
	public List getOldRecords() {
		return oldRecords;
	}

	public void setOldRecords(List oldRecords) {
		this.oldRecords = oldRecords;
	}

	public String getRecommendIdsStr() {
		return recommendIdsStr;
	}

	public void setRecommendIdsStr(String recommendIdsStr) {
		this.recommendIdsStr = recommendIdsStr;
	}

	public List<Recommend> getRecommends() {
		return recommends;
	}

	public void setRecommends(List<Recommend> recommends) {
		this.recommends = recommends;
	}

	public String getRecordIdsStr() {
		return recordIdsStr;
	}

	public void setRecordIdsStr(String recordIdsStr) {
		this.recordIdsStr = recordIdsStr;
	}

	public String getPositionSeqsStr() {
		return positionSeqsStr;
	}

	public void setPositionSeqsStr(String positionSeqsStr) {
		this.positionSeqsStr = positionSeqsStr;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public String getBeginDate() {
		return beginDate;
	}
	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public String getFieldsLabel() {
		return fieldsLabel;
	}
	public void setFieldsLabel(String fieldsLabel) {
		this.fieldsLabel = fieldsLabel;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public String getFields() {
		return fields;
	}
	public void setFields(String fields) {
		this.fields = fields;
	}
	public List getRecords() {
		return records;
	}
	public String getBeanName() {
		return beanName;
	}
	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}
	public void setRecords(List records) {
		this.records = records;
	}
	public int getRecommendTypeId() {
		return recommendTypeId;
	}
	public void setRecommendTypeId(int recommendTypeId) {
		this.recommendTypeId = recommendTypeId;
	}
	public List<RecommendType> getRecommendTypes() {
		return recommendTypes;
	}
	public void setRecommendTypes(List<RecommendType> recommendTypes) {
		this.recommendTypes = recommendTypes;
	}
	public void setRecommendService(RecommendService recommendService) {
		this.recommendService = recommendService;
	}
	
	public String fetchRecommendByRecommendTypeId(){
//		recommends = recommendService.getRecommendByRecommendTypeId(recommendTypeId);
		oldRecords = recommendService.getRecommendByRecommendTypeId(recommendTypeId);
		return SUCCESS;
	}
	
	public String getRecordsByTableName(){
		List<RecommendType> recommendTypes =recommendService.getRecommendTypeById( recommendTypeId );
		tableName = recommendTypes.get(0).getTableName();
		beanName = recommendTypes.get(0).getBeanName();
		fields = recommendTypes.get(0).getFields();
		fieldsLabel = recommendTypes.get(0).getFieldsLabel();
		records = recommendService.getRecordsByTableName(tableName,beginDate,endDate, beanName,fields);
		return SUCCESS;
	}
	
	public String updateRecommend() {
		if ( recommendIdsStr != null && !recommendIdsStr .equals("")){
			String ids[] = recommendIdsStr.split(",");
			String seqs[] = positionSeqsStr.split(",");
			for(int i=0;i<ids.length;i++){
				if( !ids[i].equals("")) {
					Recommend bean = new Recommend();
					bean.setPositionSeq(   Integer.parseInt(seqs[i])  );
					bean.setId(  Integer.parseInt(ids[i]) );
	//				bean.setRecommendTypeId(recommendTypeId);
					recommendService.updateRecommendById(bean );
				}
			}
		}
		return SUCCESS;
	}
	
	public String deleteRecommend(){
		if ( recommendIdsStr != null && !recommendIdsStr .equals("")){
			String ids[] = recommendIdsStr.split(",");
			String seqs[] = positionSeqsStr.split(",");
			for(int i=0;i<ids.length;i++){
				if ( !ids[i].equals("") ){
					Recommend bean = new Recommend();
					bean.setPositionSeq(   Integer.parseInt(seqs[i])  );
					bean.setId(  Integer.parseInt(ids[i]) );
	//				bean.setRecommendTypeId(recommendTypeId);
					recommendService.deleRecommend( bean );
				}
			}
		}		
		return SUCCESS;
	}
	
	public String saveRecommend(){
		if ( recordIdsStr != null && !recordIdsStr .equals("")){
			String ids[] = recordIdsStr.split(",");
			String seqs[] = positionSeqsStr.split(",");
			for(int i=0;i<ids.length;i++){
				if ( !ids[i].equals("")) {
					Recommend bean = new Recommend();
					bean.setPositionSeq(   Integer.parseInt(seqs[i])  );
					bean.setRecordId(  Integer.parseInt(ids[i]) );
					bean.setRecommendTypeId(recommendTypeId);
					recommendService.addRecommend(bean );
				}
			}
		}
		return SUCCESS;
	}
	@Override
	public String execute() throws Exception {
		recommendTypes = recommendService.listAllRecommendType();
		return SUCCESS;
	}

}
